package com.ning.farm_income_back.controller;

import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Land;
import com.ning.farm_income_back.dto.LandDto;
import com.ning.farm_income_back.service.LandService;
import com.ning.farm_income_back.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/land")
public class LandController {

    @Autowired
    LandService landService;

    //带条件分页查询
    @PostMapping("/page")
    public ResultVo<PageInfo> page(@RequestBody LandDto land, @RequestParam Integer pageNum, @RequestParam Integer pageSize ) {
        if(pageNum == null || pageNum < 1) pageNum = 1;

        PageInfo pageInfo = landService.getLandInfo(land,pageNum,pageSize);

        return ResultVo.success("页面数据查询成功",pageInfo);
    }

    //新增
    @PostMapping("/saveLand")
    public ResultVo saveLand(@RequestBody Land land, @RequestParam String username) {
        landService.saveLandInfo(land,username);
        return ResultVo.success("新增成功！");
    }

    //修改
    @PutMapping("/updateLand")
    public ResultVo updateLand(@RequestBody Land land) {
        landService.updateLandInfo(land);
        return ResultVo.success("修改成功！");
    }

    //删除
    @DeleteMapping("/deleteLand/{id}")
    public ResultVo deleteLand(@PathVariable Integer id) {
        landService.deleteLandInfo(id);
        return ResultVo.success("删除成功！");
    }

    //获取土地用途信息
    @GetMapping("/getLandUsageList")
    public ResultVo<List<Land>> getLandUsageList() {
        List<Land> data = landService.getAllLand();
        return ResultVo.success("获取成功！",data);
    }
}
